<?php require "includes/header.php"; ?>

<h1 class="title">De kaart:</h1>
<div class="entry">
    <?php
    $sql = "select users.user_id, user_first_name, user_last_name, x, y, user_last_update, bob from users, friends where friends.friend_user_id_1 = '" . $_SESSION['user_id'] . "' and friends.friend_user_id_2 = users.user_id and user_last_update > '" . date("Y-m-d H:i:s", time() - 21600) . "'";
    $result = mysql_query($sql);

    if (!$result) 
    {
        echo("<!-- SQL ERROR: Error performing query: " . mysql_error() . "//-->");
    }

    $num_rows = mysql_num_rows($result);

    if ($num_rows == 0) 
    {
        echo "Geen posities gevonden.";
    }
	else 
	{ ?>
		
        <p>Deze kaart bevat alle locaties van je bobcontacten (die in de afgelopen 6 uur geupdate zijn):</p>
        <div id="map_canvas" style="width: 800px; height: 600px"></div>
        
        <?php
        $output = "<script type=\"text/javascript\">\n\nfunction initialize() {\n";
        $output .= "if (GBrowserIsCompatible()) {\n";
        $output .= "var map = new GMap2(document.getElementById(\"map_canvas\"));\n";
        $output .= "map.setMapType(G_HYBRID_MAP);\n";

        $query = "SELECT x, y FROM users WHERE user_id = '" . $_SESSION['user_id'] . "';";
        $rezult = mysql_query($query);
        if (mysql_num_rows($rezult) == 1) {
            $row = mysql_fetch_array($rezult);
            $output .= "map.setCenter(new GLatLng(" . $row['x'] . ", " . $row['y'] . "), 16);\n";
            $hetBericht = "Jij zelf!";
            $jijzelf = "map.addOverlay(createMarker(new GLatLng(" . $row['x'] . "," . $row['y'] . "),'" . $hetBericht . "'));\n";
        }

        $output .= "map.addControl(new GSmallMapControl());\n";
        $output .= "map.addControl(new GMapTypeControl());\n\n";

        $output .="function createMarker(point, message) {\n";
        $output .="var marker = new GMarker(point);\n";
        $output .="GEvent.addListener(marker, \"click\", function() {\n";
        $output .="var myHtml = message;\n";
        $output .="map.openInfoWindowHtml(point, myHtml);\n";
        $output .="});\n";
        $output .="return marker;\n";
        $output .="}\n";

        echo $output;
        
        echo $jijzelf;
        
        while ($rij = mysql_fetch_array($result)) 
        {
            $hetBericht = $rij['user_first_name'] . " " . strtoupper(substr($rij['user_first_name'], 0, 1)) . ". was hier op: " . $rij['user_last_update'];
            echo "map.addOverlay(createMarker(new GLatLng(" . $rij['x'] . "," . $rij['y'] . "),'" . $hetBericht . "'));\n";
        }
        echo "\n}\n}\n\n</script>\n";
    }
    ?>
</div>
<?php require "includes/footer.php";?>